﻿using UnityEngine;
using System.Collections;
using COC;
using System;
using COC.CEvent;
using COC.Content.Task;
using COC.Window;
using COC.Character;
using COC.Service;

public class SkeletonKnight : BaseBehaviourSupport
{
		private Animator anim;
		private int MOUSE_LEFT = 0;
		private UILabel info;
		private UILabel sourceGo;
		public Transform skeletonKnight;
		CEventDriven cdriver;
		bool enableEvent = false;
		int i = 0;
		bool showText;
		bool changeStatus;

		public override void OnStart ()
		{
				this.gameObject.AddComponent<CServiceActivator> ();
				enableEvent = true;
				changeStatus = true;
		}
	
		public override void OnAwake ()
		{
				//Debug.Log(new TestService().ToString());
				//Debug.Log ("" + (((0x0001 | 8) >> 32) << 32));
				//Debug.Log(""+Convert.ToString(10,2));
				//Debug.Log(""+((CEvent.ACTION_AUDIO_PLAY>>32)<<32));
				Debug.Log (Application.platform.ToString ());
                
				anim = GetComponent<Animator> ();
				if (anim.layerCount > 1) {
						anim.SetLayerWeight (1, 1);
				}
				info = GameObject.FindGameObjectWithTag ("information_label").GetComponent<UILabel> ();
		        
				sourceGo = GameObject.FindGameObjectWithTag ("information_label2").GetComponent<UILabel> ();
				showText = true;
				//	StartCoroutine ("Draggedable");
		}
		// Use this for initialization
		//void Update()
		//{
		//	}

		void LateUpdate ()
		{
				//Draggedable ();
				//  InstantiateWithScriptByMouseOrTouch(skeletonKnight,Input.mousePosition,"MainCamera",0,"Terrain",false);
//				if (enableEvent) {
//						StartCoroutine ("CTestEventHandler");
//						enableEvent = false;
//				}



				if (changeStatus) {
						
						GameObject.FindGameObjectWithTag ("StatusMessage").GetComponent<TextMesh> ().text = "新年好";
						changeStatus = false;
				}
		}

		void OnDestroy ()
		{
				StopCoroutine ("Draggedable");

		}
		
		int touched = 0;
		int id = 0;
		// Update is called once per frame
		void Draggedable ()
		{
				#if UNITY_ANDROID_1
		//info.text=Input.touches.Length+"";
		if(Input.touchCount==1){
			if(Input.GetTouch(i).phase==TouchPhase.Began){
				info.text="touched:"+Input.touches.Length;
				GameObject go=Instantiate(this.gameObject,transform.position,transform.rotation) as GameObject;
				go.transform.Translate(new Vector3(this.gameObject.transform.position.x+UnityEngine.Random.value*10,
				                                   this.gameObject.transform.position.y,
				                                   this.gameObject.transform.position.z));
				//go.transform.position.x=(this.gameObject.transform.position.x+UnityEngine.Random.value*10);
				Debug.Log("Have Touched "+touched+"Times");
			}
		}
				#elif UNITY_IPHONE
		
				#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
		
				#else
		
				#endif
			
				
		}

		IEnumerator CTestEventHandler ()
		{
				if (!enableAllScripts) {
						CEventHandler handler = new CEventHandler (CEventType.UI_EVENT, this, CEvent.ACTION_UI_DEFAULT);
						GameObject go = GameObject.FindGameObjectWithTag (CEventDriven.ALIAS_NAME);
						if (go != null) {

						} else {
								go = GameObject.Find (CEventDriven.ALIAS_NAME);

						}
						cdriver = go.GetComponent<CEventDriven> ();
						cdriver.HandleMessage (handler);
						CEventHandler handler1 = new CEventHandler (CEventType.UI_EVENT, this, CEvent.ACTION_UI_DEFAULT);
						handler1.action = CEvent.ACTION_AUDIO_PAUSE;
						cdriver.HandleMessage (handler1);
						CEventHandler handler2 = new CEventHandler (CEventType.UI_EVENT, this, CEvent.ACTION_UI_DEFAULT);
						handler2.action = CEvent.ACTION_UI_DISABLE;
						cdriver.HandleMessage (handler2);
						CEventHandler handler3 = new CEventHandler (CEventType.UI_EVENT, this, CEvent.ACTION_UI_DEFAULT);
						handler3.action = CEvent.ACTION_UI_UPDATE_DATA;
						cdriver.HandleMessage (handler3);
						cdriver.getAvailableServicesCount ();
						cdriver.RemoveService (typeof(TestService));
						cdriver.getAvailableServicesCount ();
						//reset to avoid to duplicate repeatedly
						enableEvent = false;
						yield return cdriver;
				}
				yield return null;
		}
}
